<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="商品等级" prop="productLevel">
        <el-select
          v-model="queryParams.productLevel"
          placeholder="请选择商品等级"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in DictData.levelList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams.goodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="一级分类" prop="classOneId">
        <el-select
          v-model="queryParams.classOneId"
          placeholder="请选择一级分类"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in classifyList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="是否上架" prop="isInitiate">
        <el-select
          v-model="queryParams.isInitiate"
          placeholder="请选择是否上架"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option label="上架" :value="1"> </el-option>
          <el-option label="下架" :value="2"> </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="goodOpen = true"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['system:good:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['system:good:remove']">删除</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:good:export']"
            >导出</el-button
          >
        </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!--  1vip 3共享店主 4联创 5区域运营 6总裁 -->
    <el-table
      v-loading="loading"
      :data="goodList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="商品编号" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="goodName" />
      <!-- <el-table-column label="一级分类ID" align="center" prop="classOneId">
        <template slot-scope="{ row }">
          {{ row.classOneId }}
        </template>
      </el-table-column>

      <el-table-column label="一级分类" align="center" prop="classOneId">
        <template slot-scope="{ row }">
          {{ forrmatterEnmu(classifyList, row.classOneId) }}
        </template>
      </el-table-column> -->
      <el-table-column label="售价" align="center" prop="retailPrice" />
      <!-- <el-table-column label="商品库存" align="center" prop="goodInventory" /> -->
      <!-- <el-table-column label="虚拟销量" align="center" prop="goodInventory" /> -->
      <!-- <el-table-column label="是否上架" align="center" prop="isInitiate">
        <template slot-scope="{ row }">
          {{ row.isInitiate == 1 ? "上架" : "下架" }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="商品等级" align="center" prop="productLevel">
        <template slot-scope="{ row }">
          {{ forrmatterEnmu(DictData.levelList, row.productLevel) }}
        </template>
      </el-table-column> -->
      <el-table-column label="商品类型" align="center" prop="classOneName">
        <template slot-scope="{ row }">
          {{ row.goodType == "1" ? "实体商品" : "虚拟商品" }}
        </template>
      </el-table-column>
      <el-table-column label="商品主图" align="center" prop="goodImg">
        <template slot-scope="{ row }">
          <image-preview :src="row.goodImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="添加时间" align="center" prop="createTime">
      </el-table-column> -->
      <!-- <el-table-column label="商品标签" align="center" prop="goodTag" /> -->
      <!-- <el-table-column label="省市区" align="center" prop="mergerName" /> -->
      <!-- <el-table-column label="库存预警" align="center" prop="inventoryAlert" /> -->
      <!-- <el-table-column label="库存显示" align="center" prop="inventoryShown" /> -->
      <!-- <el-table-column label="销量显示" align="center" prop="salesShown" /> -->
      <!-- <el-table-column label="虚拟销量" align="center" prop="virtualSales" /> -->
      <!-- <el-table-column label="是否置顶推荐" align="center" prop="isTop" /> -->
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:good:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:good:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择商品列表 -->
    <el-dialog
      title="新增商品"
      :visible.sync="goodOpen"
      width="800px"
      append-to-body
    >
      <GoodList @tableSelection="tableSelection"></GoodList>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGood,
  getGood,
  delGood,
  addGood,
  updateGood,
  listGoodVo,
} from "@/api/system/goods/list";
import { goodClassifyList } from "@/api/system/goods/classify";
import { forrmatterEnmu } from "@/utils/index";

import GoodList from "./components/goodList.vue";

export default {
  name: "Good",
  components: { GoodList },

  data() {
    return {
      goodOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商商品表格数据
      goodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodName: null,
        ifCloudGood: 1,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      classifyList: [],
      classifyList1: [],
    };
  },
  created() {
    this.getList();
    this.getClassList();
  },
  methods: {
    forrmatterEnmu,
    /** 查询供应商商品列表 */
    getList() {
      this.loading = true;
      listGoodVo(this.queryParams).then((response) => {
        this.goodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 选择商品
    tableSelection(v) {
      let obj = {
        ifCloudGood: 1,
        id: v[0].id,
      };
      updateGood(obj).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.goodOpen = false;

        this.getList();
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodId: null,
        productLevel: null,
        goodName: null,
        retailPrice: null,
        vipPrice: null,
        svipPrice: null,
        sharePrice: null,
        joinPrice: null,
        operationsPrice: null,
        presidentPrice: null,
        singlePrize: null,
        prizePool: null,
        classOneId: null,
        goodType: null,
        classOneName: null,
        subtitle: null,
        goodImg: null,
        goodBannerImg: null,
        goodVideo: null,
        goodVideoImg: null,
        goodTag: null,
        goodService: null,
        isInitiate: "1",
        mallDisplay: null,
        regionClass: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        region: null,
        regionString: null,
        mergerName: null,
        goodDesc: null,
        goodSpecs: null,
        originalPrice: null,
        costPrice: null,
        goodUnit: null,
        goodInventory: null,
        inventoryAlert: null,
        goodWeight: null,
        productCode: null,
        inventoryShown: null,
        salesShown: null,
        virtualSales: null,
        inventoryVersion: null,
        classTwoId: null,
        classTwoName: null,
        classThreeId: null,
        classThreeName: null,
        classAncestors: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        memberBuyingRights: null,
        participatingMemberPrice: "1",
        pointsBonus: null,
        bonusSet: null,
        bounsValue: null,
        bonusLevel: null,
        pointsRedemption: null,
        pointsUse: null,
        redemptionSet: null,
        redemptionValue: null,
        isTop: null,
      };
      this.resetForm("form");
    },
    classChange(v) {
      if (v == 10) {
        this.rules.productLevel = [
          { required: true, message: "商品等级不能为空", trigger: "blur" },
        ];
      } else {
        this.rules.productLevel = null;
      }

      if (v != 2) {
        this.form.vipPrice = "";
        this.form.svipPrice = "";
        this.form.sharePrice = "";
        this.form.joinPrice = "";
        this.form.operationsPrice = "";
        this.form.presidentPrice = "";
      }
      this.classifyList.forEach((item) => {
        if (item.value == v) {
          this.form.classOneName = item.name;
        }
      });
    },

    productLevelChange(v) {
      console.log("888", v);
    },
    /**获取一级分类列表 */
    async getClassList() {
      const params = {
        pageNum: 1,
        pageSize: 100000,
      };
      let res = await goodClassifyList(params);
      if (res.code == 200) {
        this.classifyList = [];
        this.classifyList1 = [];
        res.rows.forEach((item) => {
          if (item.deptName != "积分商品") {
            this.classifyList1.push({
              name: item.deptName,
              value: item.id,
            });
          }

          this.classifyList.push({
            name: item.deptName,
            value: item.id,
          });
        });
      }
    },
    /**图片上传 */
    uploadImg(file) {
      this.form.goodImg = file;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGood(id).then((response) => {
        this.form = response.data;
        this.form.memberBuyingRights = response.data.memberBuyingRights - 0;
        this.open = true;
        this.title = "修改供应商商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGood(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGood(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let obj = {
        ifCloudGood: 0,
        id: row.id,
      };
      updateGood(obj).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.goodOpen = false;

        this.getList();
      });

      //   const ids = row.id || this.ids;
      //   this.$modal
      //     .confirm("是否确认删除？")
      //     .then(function () {
      //       return delGood(ids);
      //     })
      //     .then(() => {
      //       this.getList();
      //       this.$modal.msgSuccess("删除成功");
      //     })
      //     .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/admin/good/export",
        {
          ...this.queryParams,
        },
        `good_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="less" scoped>
.base-title {
  font-size: 18rpx;
  color: #1a1a1a;
  display: flex;
  align-items: center;
  margin-bottom: 15px;

  &::before {
    content: "";
    display: inline-block;
    width: 3px;
    height: 15px;
    background: #1777ff;
    border-radius: 3px;
    margin-right: 8px;
  }
}
</style>
